package com.xjrh.traintravel.collector.runner;

import com.xjrh.traintravel.collector.netty.command.file.FileList;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 数据缓存工具
 *
 * @author snps
 * @create 2022-02-18 14:26
 * @describe
 **/
@Component
@Order(value = 3)
@Slf4j
public class DataCacheTool implements CommandLineRunner {

    /**
     * 升级文件类型：主机、DSP、前置
     */
    public static final String UPGRADE_FILE_HOST = "UPGRADE_FILE_HOST";
    public static final String UPGRADE_FILE_DSP = "UPGRADE_FILE_DSP";
    public static final String UPGRADE_FILE_FRONT = "UPGRADE_FILE_FRONT";

    public static final String FILE_LIST = "FILE_LIST";
    public static final String FILE_CONTENT = "FILE_CONTENT";


    /**
     * 缓存集合-升级文件内容
     *  <p>
     *      key值规则：设备标识(mac地址)#升级文件类型
     *  </p>
     */
    private Map<String, List<String>> upgradeFileContentCache;

    /**
     * 缓存集合-升级文件进度（行数）
     *  <p>
     *      key值规则：设备标识(mac地址)#升级文件类型
     *  </p>
     */
    private Map<String, Integer> upgradeFileRateCache;

    /**
     * 缓存集合-设备上的文件列表
     *  <p>
     *      key值规则：设备标识(mac地址)#FILE_LIST
     *  </p>
     */
    private Map<String, FileList> fileListCache;

    /**
     * 缓存集合-文件内容
     *  <p>
     *      key值规则：设备标识(mac地址)#FILE_CONTENT
     *  </p>
     */
    private Map<String, List<String>> fileContentCache;


    @Override
    public void run(String... args) throws Exception {
        log.warn("****************************************");
        upgradeFileContentCache = new ConcurrentHashMap<>();
        log.warn("init UpgradeFileContentCache...success");

        upgradeFileRateCache = new ConcurrentHashMap<>();
        log.warn("init UpgradeFileRateCache...success");

        fileListCache = new ConcurrentHashMap<>();
        log.warn("init FileListCache...success");

        fileContentCache = new ConcurrentHashMap<>();
        log.warn("init FileContentCache...success");
        log.warn("****************************************");
    }

    /**
     * 获取升级文件类型
     * @param equipmentType 设备类型（0-主机，1-dsp，2-前置）
     * @return String
     */
    public String getUpgradeFileType(Integer equipmentType) {
        if (equipmentType == null || equipmentType < 0 || equipmentType > 2) {
            return null;
        }

        if (equipmentType == 0) {
            return DataCacheTool.UPGRADE_FILE_HOST;
        }
        if (equipmentType == 1) {
            return DataCacheTool.UPGRADE_FILE_DSP;
        }
        if (equipmentType == 2) {
            return DataCacheTool.UPGRADE_FILE_FRONT;
        }
        return null;
    }


    /************************************************************
     * 设置 升级文件 缓存
     * @param key
     * @param value
     */
    public void putUpgradeFileContentCache(String key, List<String> value) {
        upgradeFileContentCache.put(key, value);
        // log.info("UpgradeFileContentCache add: {} success", key);
        // log.info("UpgradeFileContentCache size: {}", upgradeFileContentCache.size());
    }

    /**
     * 获取 升级文件 缓存
     * @param key
     * @return
     */
    public List<String> getUpgradeFileContentCache(String key) {
        return upgradeFileContentCache.get(key);
    }

    /**
     * 删除 升级文件 缓存
     * @param key
     */
    public void removeUpgradeFileContentCache(String key) {
        if (upgradeFileContentCache.containsKey(key)) {
            upgradeFileContentCache.remove(key);
            log.info("UpgradeFileContentCache remove: {} success", key);
        }

        log.info("UpgradeFileContentCache cache: {} null", key);
        log.info("UpgradeFileContentCache size: {}", upgradeFileContentCache.size());
    }


    /************************************************************
     * 设置 升级文件-进度 缓存
     * @param key
     * @param value
     */
    public void putUpgradeFileRateCache(String key, Integer value) {
        upgradeFileRateCache.put(key, value);
        // log.info("UpgradeFileRateCache add: {} success", key);
        // log.info("UpgradeFileRateCache size: {}", value);
    }

    /**
     * 获取 升级文件 缓存
     * @param key
     * @return
     */
    public Integer getUpgradeFileRateCache(String key) {
        return upgradeFileRateCache.get(key);
    }

    /**
     * 删除 升级文件-进度 缓存
     * @param key
     */
    public void removeUpgradeFileRateCache(String key) {
        if (upgradeFileRateCache.containsKey(key)) {
            upgradeFileRateCache.remove(key);
            log.info("UpgradeFileRateCache remove: {} success", key);
        }

        log.info("UpgradeFileRateCache cache: {} null", key);
        log.info("UpgradeFileRateCache size: {}", upgradeFileRateCache.size());
    }


    /************************************************************
     * 设置 设备上的文件列表 缓存
     * @param key
     * @param value
     */
    public void putFileListCache(String key, FileList value) {
        fileListCache.put(key, value);
        log.info("FileListCache add: {} success", key);
        log.info("FileListCache size: {}", fileListCache.size());
    }

    /**
     * 获取 设备上的文件列表 缓存
     * @param key
     * @return
     */
    public FileList getFileListCache(String key) {
        return fileListCache.get(key);
    }

    /**
     * 删除 设备上的文件列表 缓存
     * @param key
     */
    public void removeFileListCache(String key) {
        if (fileListCache.containsKey(key)) {
            fileListCache.remove(key);
            log.info("FileListCache remove: {} success", key);
        }

        log.info("FileListCache cache: {} null", key);
        log.info("FileListCache size: {}", fileListCache.size());
    }

    /************************************************************
     * 设置 设备获取文件内容 缓存
     * @param key
     * @param value
     */
    public void putFileContentCache(String key, List<String> value) {
        fileContentCache.put(key, value);
        log.info("FileContentCache add: {} success", key);
        log.info("FileContentCache size: {}", fileContentCache.size());
    }

    /**
     * 获取 设备获取文件内容 缓存
     * @param key
     * @return
     */
    public List<String> getFileContentCache(String key) {
        return fileContentCache.get(key);
    }

    /**
     * 删除 设备获取文件内容 缓存
     * @param key
     */
    public void removeFileContentCache(String key) {
        if (fileContentCache.containsKey(key)) {
            fileContentCache.remove(key);
            log.info("FileContentCache remove: {} success", key);
        }

        log.info("FileContentCache cache: {} null", key);
        log.info("FileContentCache size: {}", fileContentCache.size());
    }

}